package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.*;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    DishService dishService;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    RedisTemplate redisTemplate;

    //添加套餐
    @PostMapping
    public R sava(@RequestBody SetmealDto setmealDto){

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmealService.save(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(id);
            setmealDishService.save(dish);
        }
        /**
         * 添加套餐
         */
        redisTemplate.delete("setmeal"+setmealDto.getCategoryId()+"_"+setmealDto.getStatus());
        return R.success("保存套餐成功");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        //创建分页
        Page<Setmeal> pages = new Page<>(page,pageSize);
        //条件构造器
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(name!=null&&name!="",Setmeal::getName,name);
        //获取所有套餐对象集合
        Page<Setmeal> page1 = setmealService.page(pages, wrapper);
        List<Setmeal> records = page1.getRecords();

        List<SetmealDto> listDto = new ArrayList<>();
        //创建SetmealDto集合
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            setmealDto.setCategoryName(record.getName());
            listDto.add(setmealDto);
        }
        Page page2 = new Page();
        Page page3 = page2.setRecords(listDto);
        return R.success(page3);
    }

    //删除套餐
    @DeleteMapping
    public R delete(Long[] ids){
        List<Long> asList = Arrays.asList(ids);
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmeals) {
            if(setmeal.getStatus()==1){
                return R.error("启售中无法删除");
            }
        }
        setmealService.removeByIds(asList);
        /**
         * 删除套餐
         */
        Set keys = redisTemplate.keys("setmeal_*");
        redisTemplate.delete(keys);
        return R.success("删除成功");
    }
    //套餐启停
    @PostMapping("/status/{status}")
    public R statu(@PathVariable Integer status,Long[] ids){

        //获取对象集合
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Setmeal::getId,ids);
        List<Setmeal> setmeals = setmealService.list(wrapper);
        int s1 = status==1 ? 1 : 0;
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(s1);
            setmealService.updateById(setmeal);
        }

        return R.success("操作成功");
    }

    //修改套餐--1根据id查询套餐数据
    @GetMapping("/{id}")
    public R update(@PathVariable Long id){
        Setmeal byId = setmealService.getById(id);
        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(byId,dto);
        dto.setCategoryName(byId.getName());
        //根据id获取套餐下所有的菜品
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        dto.setSetmealDishes(list);
        redisTemplate.delete("setmeal_"+id+"_"+dto.getStatus());
        return R.success(dto);
    }
    //修改套餐
    @PutMapping
    public R update2(@RequestBody SetmealDto setmealDto){

        //根据传过来的对象获取id属性
        Long id = setmealDto.getId();
        //根据id获取套餐下所有的菜品
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SetmealDish::getSetmealId,id);
        //List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDishService.remove(wrapper);
        //setmealDishService.removeByIds(list);

        //创建新的setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmealService.updateById(setmeal);

        Long aLong = setmeal.getId();
        //将对象中的套餐餐品信息存入套餐中
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(aLong);
            setmealDishService.save(dish);
        }
        /**
         * 修改套餐
         */
        redisTemplate.delete("setmeal"+setmealDto.getCategoryId()+"_" + setmealDto.getStatus());

        return R.success("修改成功");

    }
    //客户端查询所有套餐信息
    @GetMapping("/list")

    public R list(Long categoryId,Integer status){
        /**
         * 查询所有
         */
        List<Setmeal> o = (List<Setmeal>) redisTemplate.opsForValue().get("setmeal"+categoryId + "_" + status);
        if(o != null){
            return R.success(o);
        }
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Setmeal::getCategoryId,categoryId);
        wrapper.lambda().eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(wrapper);
        redisTemplate.opsForValue().set("setmeal"+categoryId+"_" +status,list);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R getDish(@PathVariable Long id){
        /*Setmeal byId = setmealService.getById(id);

        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);

        SetmealDto SetmealDto = new SetmealDto();
        BeanUtils.copyProperties(SetmealDto,byId);
        SetmealDto.setSetmealDishes(list);
        return R.success(SetmealDto);*/
        List<DishDto> dishDtos = new ArrayList<>();
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        for (SetmealDish setmealDish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(setmealDish,dishDto);
            QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(DishFlavor::getDishId,setmealDish.getDishId());
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(list1);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);



    }

}
